package com.dabai.app.im;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;

import de.greenrobot.event.EventBus;


/**
 * The abstract base activity.
 */
public abstract class BaseActivity extends AppCompatActivity {

    protected LinearLayout mLoadingView;
    private LinearLayout mNodataView;
    protected Activity mActivity;
    private boolean mIsPaused;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initFinshFilter();
        mActivity = this;
        createActivityView(savedInstanceState);
    }

    public abstract void createActivityView(Bundle savedInstanceState);

    @Override
    public void addContentView(View view, ViewGroup.LayoutParams params) {
        super.addContentView(view, params);
    }

    private ViewGroup createLoadingLayout() {
        if (mLoadingView != null) {
            return mLoadingView;
        }

        mLoadingView = (LinearLayout) ViewUtils.getItemView(this, R.layout.item_loading);
        mLoadingView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Nothing
            }
        });
        mLoadingView.setBackgroundResource(android.R.color.transparent);
        return mLoadingView;
    }

    protected void showLoading() {
        ViewGroup rootView = (ViewGroup) this.getWindow().getDecorView();
        if (null != mLoadingView) {
            rootView.removeView(mLoadingView);
        }
        rootView.addView(createLoadingLayout());
    }

    protected void hiddenLoading() {
        ViewGroup rootView = (ViewGroup) this.getWindow().getDecorView();
        rootView.removeView(mLoadingView);
    }

    private void initFinshFilter() {
        IntentFilter filter = new IntentFilter();
        filter.addAction("finish");
        registerReceiver(mFinishReceiver, filter);
    }

    private BroadcastReceiver mFinishReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if ("finish".equals(intent.getAction())) {
                Log.e("#########", "I am " + getLocalClassName()
                        + ",now finishing myself...");
                finish();
            }
        }
    };

    // 点击EditText文本框外任何地方隐藏键盘
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (InputManagerUtils.isShouldHideInput(v, ev)) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
                postOnHideSoftInputEvent();
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    long lastHideTime = 0;

    private synchronized void postOnHideSoftInputEvent() {
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastHideTime > 1000) {
            EventBus.getDefault().post(new OnHideSoftInputEvent());
            lastHideTime = currentTime;
        }
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return super.onSupportNavigateUp();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mFinishReceiver);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mIsPaused = false;
    }

    @Override
    protected void onPause() {
        mIsPaused = true;
        super.onPause();
    }

    public int getResourceColor(int colorId) {
        return getResources().getColor(colorId);
    }

    public Drawable getResourceDrawable(int drawableId) {
        return getResources().getDrawable(drawableId);
    }

    // Event
    public static class OnHideSoftInputEvent {
    }
}
